package com.tz.cartelo;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.tz.common.util.JsonUtils;
import com.tz.common.util.PreferencesUtils;
import com.tz.common.util.VolleyDataUtils;
import com.tz.common.util.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Base application
 * Created by Chris on 16/7/18.
 */
public class BaseApplication extends Application {

    private static BaseApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        if (instance == null) {
            instance = this;
        }
//        NoHttp.initialize(this);
        //图片查看初始化
        Fresco.initialize(getApplicationContext());
        //Init preferences.
        PreferencesUtils.initPreferences(this);
        VolleyDataUtils.init(this);
        JsonUtils.init(this);
        Globals.init(this);

        //Set debug mode or not.
        LogUtil.setDebug(BuildConfig.LOG_DEBUG);
        initImageLoader(getApplicationContext());
        //bug提交初始化
//        CrashReport.initCrashReport(getApplicationContext(), "4760e26e-854c-45e8-97fe-f62a26716688", false);
//        regToWx();
        //极光推送
//        JPushInterface.setDebugMode(true);  // 设置开启日志,发布时请关闭日志
//        JPushInterface.init(this);            // 初始化 JPush
    }

//    private void regToWx() {
//        api = WXAPIFactory.createWXAPI(this, APP_ID, true);
//        api.registerApp(APP_ID);
//    }

    public static BaseApplication getInstance() {
        return instance;
    }

    public void initImageLoader(Context context) {
        // 缓存文件的目录
        File cacheDir = StorageUtils.getOwnCacheDirectory(context,
                getString(R.string.file_name));
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context).threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory().diskCacheFileCount(100)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(50 * 1024 * 1024)// 50 Mb
                .memoryCacheSize(50 * 1024 * 1024)// 内存缓存的最大值
                .diskCache(new UnlimitedDiscCache(cacheDir))// 自定义缓存路径
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs() // Remove for release app
                .build();
        ImageLoader.getInstance().init(config);
    }

    private static List<Activity> lists = new ArrayList<>();

    public static void addActivity(Activity activity) {
        lists.add(activity);
    }

    public static void clearActivity() {
        if (lists != null) {
            for (Activity activity : lists) {
                activity.finish();
            }
            lists.clear();
        }
    }
}
